package com.mysql.test;

import com.mysql.cj.jdbc.Driver;
import entity.User;
import org.junit.Test;
import utils.AddDataUtil;

import javax.naming.spi.DirectoryManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class AddData {
    @Test
    public void addData() throws SQLException {
        int i = AddDataUtil.addData(8, "李四", 90, "23131");
        if (i>0){
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }
    @Test
    public void insertData() throws SQLException {
        User user = new User();
        user.setId(9);
        user.setName("王五");
        user.setAge(100);
        user.setIphone("345588677");
        int i = AddDataUtil.insertData(user);
        if (i>0){
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }
    @Test
    public void insertParam() throws SQLException {
        String sql="insert into user values(?,?,?,?)";
        List list=new ArrayList();
        list.add(11);
        list.add("赵六");
        list.add(100);
        list.add("123456789");
        int i = AddDataUtil.insertParam(sql,list);
        if (i>0){
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }
    @Test
    public void insertParam2() throws SQLException {
        String sql="update user set name=?,age=?,iphone=? where id=?";
        List list=new ArrayList();
        list.add("小花");
        list.add(10);
        list.add("323232");
        list.add(1);
        int i = AddDataUtil.insertParam(sql,list);
        if (i>0){
            System.out.println("修改成功");
        }
        else {
            System.out.println("修改失败");
        }
    }
    @Test
    public void insertParam3() throws SQLException {
        String sql="insert into user values(?,?,?,?)";
        String [] params = new String[]{"12", "小丽", "20", "123456789"};
        int i = AddDataUtil.insertParamByObject(sql, (Object) params);
        if (i>0){
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }
    @Test
    public void insertParam4() throws SQLException {
        String sql="update user set name=?,age=?,iphone=? where id=?";

        int i = AddDataUtil.insertParamByObject(sql,"小花",13,"123489",12);
        if (i>0){
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }
}
